function getInfo(){
     FB.api('/me', function(response) {
         alert('Your name is ' + response.name);
     });
}

function exibeDados()
{
    FB.api('/me', function(response) {
       $("#image").attr("src","https://graph.facebook.com/"+ response.id + "/picture?type=normal");
       $("#image").attr("style","box-shadow:inset 0 0 4px #000000;-moz-box-shadow:inset 0 0 40px #000000;border-radius:10px;float:right;");
       $("#medalha").html("<div class='lado-direito-medalha'>7</div>");
       $("#nome").html("<h4>"+response.first_name+"</h4><b>3</b> followers<br /><b>2</b> following");
       //$("#email").html(response.email);
       $("#informacoes").css("display","block");
     });
}

function getListProducts(pesquisa)
{
    $("#listProduct").empty();
    $("#listProduct").append("<ul class='thumbnails'>");
    $.ajax({
       type: 'get',
       data: 'keyword='+pesquisa+'&method=list&format=json',
       datatype: 'json',
       url:'product',
       success: function(retorno){
           var json = $.parseJSON(retorno);
           var data = json.product;
           var i=0;
             for (i=0;i<data.length;i++){
                 addProductToList(data[i].product);
             }
       }
     });
     $("#listProduct").append("</ul>");
}

function addProductToList(product){  
   var url = product.links[0].link.url;
   /*$("#listProduct").append("<a href="+url+"><div class='product'><img src="+product.thumbnail.url+"><span class='name'>"+product.productname+"</span><span class='price'>R$ "+product.pricemin+"</span><button>Adicionar Produto na Loja</button></div></a>")*/
   
   $("#listProduct").append("<li class='span4 center'><div class='thumbnail fixedHeight'><img class='produto' src='"+product.thumbnail.url+"' alt=''><h3 class='tituloProduto'>"+product.productname+"</h3><p class='justify'>Celular muito legal, moderno, que voce pode usar para baixar joguinhos, aplicativos e outras coisas super divertidas! Compre ja o seu!</p><div class='botoes'><div class='buttons pull-left'><div class='btn-group trintapx' data-toggle='buttons-checkbox'><button type='button' class='btn'><img src='layout/comment.png' alt='' /></button><button type='button' class='btn fonte-pequena'>5</button></div></div><div class='buttons pull-left'><div class='btn-group trintapx' data-toggle='buttons-checkbox'><button type='button' class='btn'><img src='layout/like.png' alt='' /></button><button type='button' class='btn fonte-pequena'>50</button></div></div><div class='buttons pull-left'><div class='btn-group trintapx' data-toggle='buttons-checkbox'><button type='button' class='btn'><img src='layout/clique.png' alt='' /></button><button type='button' class='btn fonte-pequena'>10</button></div></div></div></div></li>");
}
function cadastrarLoja(){
     FB.login(function(response){
     },{ scope: 'email,user_likes,read_stream,read_friendlists,publish_stream'});
     
     FB.api('/me', function(response) {
        setPoints(response);
     });
     
     FB.api('/me', function(response) {
        createStore(response);
     });
     
     //location.href = "loja.jsp";
}

function createStore(response){
    $.ajax({
       type: 'get',
       data: 'profileid='+response.id+'&email='+response.email+'&name='+response.name,
       datatype: 'json',
       url:'store',
       success: function(retorno){
           //url: 'loja.jsp';
       }
     });}
     
function setPoints(response){
    $.ajax({
       type: 'get',
       data: 'profileid='+response.id+'&points='+"100",
       datatype: 'json',
       url:'points',
       success: function(retorno){
           //Implementar
       }
     });}
  
  window.fbAsyncInit = function() {
           FB.init({
           appId      : '414886968534358', // App ID
           channelUrl : '//localhost/channel.html', // Channel File
           status     : true, // check login status
           cookie     : true, // enable cookies to allow the server to access the session
           xfbml      : true  // parse XFBML
        });
           FB.Event.subscribe('auth.login', function(response) {
             $("#login").css("display","none");
             $("#logout").css("display","inline");
             $("#login2").css("display","none");
             $("#logout2").css("display","inline");
             exibeDados();
           });
           FB.Event.subscribe('auth.logout', function(response) {
             $("#login").css("display","inline");
             $("#logout").css("display","none");
             $("#login2").css("display","inline");
             $("#logout2").css("display","none");
             $("#informacoes").css("display","none");
             window.location = 'index.jsp';
           });// Additional initialization code here
           
        };
        
       // Load the SDK Asynchronously
       (function(d){
          var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
          if (d.getElementById(id)) {return;}
          js = d.createElement('script'); js.id = id; js.async = true;
          js.src = "//connect.facebook.net/en_US/all.js";
          ref.parentNode.insertBefore(js, ref);
        }(document));